package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.admin.bo.AdminCreateBo;
import com.cskaoyan.market.db.admin.bo.AdminUpdateBo;
import com.cskaoyan.market.service.AdminUserService;
import com.cskaoyan.market.db.admin.vo.AdminCreateVo;
import com.cskaoyan.market.db.admin.vo.AdminUpdateVo;
import com.cskaoyan.market.db.admin.vo.ListAdminVo;
import com.cskaoyan.market.db.common.CommonData;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.util.Md5Utils;
import com.cskaoyan.market.util.MyBatisUtil;
import com.cskaoyan.market.util.ReflectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 小焦
 * @since 2023/03/18 09:31
 */
public class AdminUserServiceImpl implements AdminUserService {

    @Override
    public CommonData<ListAdminVo> list(Integer page, Integer limit, String sort, String order, String username) {
        // 调用dao代码来进行查询
        // SqlSession一定不能写成成员变量
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);

        // 开启分页插件
        PageHelper.startPage(page, limit);
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username.trim() + "%");
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(sort + " " + order);

        List<MarketAdmin> marketAdmins = adminMapper.selectByExampleSelective(example);

        // // 获取查询到的总数据量
        // long total = pageInfo.getTotal();
        // System.out.println("总数据量 " + total);
        // 获取当前页的数据量
        System.out.println("marketAdmins.size() = " + marketAdmins.size());
        List<ListAdminVo> dashboardVos = new ArrayList<>();
        for (MarketAdmin marketAdmin : marketAdmins) {
            ListAdminVo dashboardVo = new ListAdminVo();
            dashboardVo.setId(marketAdmin.getId());
            dashboardVo.setUsername(marketAdmin.getUsername());
            dashboardVo.setAvatar(marketAdmin.getAvatar());
            dashboardVo.setRoleIds(Arrays.asList(marketAdmin.getRoleIds()));
            dashboardVos.add(dashboardVo);
        }
        PageInfo<ListAdminVo> pageInfo = new PageInfo<>(dashboardVos);
        CommonData<ListAdminVo> data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public AdminCreateVo create(AdminCreateBo adminCreateBo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();

        criteria.andUsernameEqualTo(adminCreateBo.getUsername());

        criteria.andDeletedEqualTo(false);
        MarketAdmin admin = adminMapper.selectOneByExample(marketAdminExample);
        if (admin != null) {
            return null;
        }
        // 通过md5 来加密 管理员密码 , 避免数据库密码的明文
        String password = Md5Utils.getMd5(adminCreateBo.getPassword(), adminCreateBo.getUsername());
        adminCreateBo.setPassword(password);

        MarketAdmin marketAdmin = ReflectUtils.copyObject(adminCreateBo, MarketAdmin.class);
        LocalDateTime now = LocalDateTime.now();
        // marketAdmin.setLastLoginIp(req.getRemoteAddr());
        // marketAdmin.setLastLoginTime(now);

        marketAdmin.setAddTime(now);

        marketAdmin.setUpdateTime(now);

        // System.out.println(marketAdmin);
        int row = adminMapper.insertSelective(marketAdmin);

        // 关闭SqlSession
        sqlSession.commit();
        sqlSession.close();

        AdminCreateVo adminCreateVo = ReflectUtils.copyObject(marketAdmin, AdminCreateVo.class);
        return adminCreateVo;


    }

    @Override
    public AdminUpdateVo update(AdminUpdateBo adminUpdateBo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andUsernameEqualTo(adminUpdateBo.getUsername());
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(marketAdminExample);
        if (marketAdmins != null&&marketAdmins.size()>1) {
            return null;
        }

        // 通过md5 来加密 管理员密码 , 避免数据库密码的明文
        String password = Md5Utils.getMd5(adminUpdateBo.getPassword(), adminUpdateBo.getUsername());
        adminUpdateBo.setPassword(password);
        MarketAdmin marketAdmin = ReflectUtils.copyObject(adminUpdateBo, MarketAdmin.class);

        MarketAdmin sourceAdmin = adminMapper.selectByPrimaryKey(adminUpdateBo.getId());
        LocalDateTime now = LocalDateTime.now();
        marketAdmin.setUpdateTime(now);
        // 如果 相等的话没有必要修改
        if (sourceAdmin.getUsername().equals(marketAdmin.getUsername())
            && sourceAdmin.getPassword().equals(marketAdmin.getPassword())
            && sourceAdmin.getAvatar().equals(marketAdmin.getAvatar())
            && sourceAdmin.getRoleIds().equals(marketAdmin.getRoleIds())) {
            sqlSession.commit();
            sqlSession.close();
        } else {
            // 更新
            adminMapper.updateByPrimaryKeySelective(marketAdmin);
        }

        sqlSession.commit();
        sqlSession.close();
        AdminUpdateVo adminUpdateVo = ReflectUtils.copyObject(marketAdmin, AdminUpdateVo.class);
        return adminUpdateVo;
    }

    @Override
    public void delete(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdmin marketAdmin = new MarketAdmin();
        LocalDateTime now = LocalDateTime.now();
        marketAdmin.setId(id);
        marketAdmin.setUpdateTime(now);
        marketAdmin.setDeleted(true);
        adminMapper.updateByPrimaryKeySelective(marketAdmin);
        sqlSession.commit();
        sqlSession.close();
    }
}
